﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class FarmersForm : Form
    {

        int regID, avpID;
        bool updaterec = true;
        TextBox tbNewFarmer = new TextBox();

        tb_farm farmer;

        public FarmersForm()
        {
            InitializeComponent();
        }

        private void FarmersForm_Load(object sender, EventArgs e)
        {
            if (InformationClass.isAdmin)
            {
                cbRegions.SelectedIndexChanged -= cbRegions_SelectedIndexChanged;
                cbRegions.DataSource = UtilClass.FillCBRegions();
                cbRegions.DisplayMember = "Name";
                cbRegions.ValueMember = "ID";
                cbRegions.SelectedIndex = -1;
                cbRegions.Text = string.Empty;
                cbRegions.SelectedIndexChanged += new EventHandler(cbRegions_SelectedIndexChanged);
            }
            else
            {
                regID = InformationClass.regionID;
                cbRegions.Enabled = false;
                cbAVP.Enabled = true;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.DisplayMember = "Name";
                cbAVP.ValueMember = "ID";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
            }
        }

        private void cbRegions_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
            regID = Convert.ToInt32(cbRegions.SelectedValue);
            cbAVP.Enabled = true;
            cbAVP.DataSource = UtilClass.FillAVP(regID);
            cbAVP.DisplayMember = "Name";
            cbAVP.ValueMember = "ID";
            cbAVP.SelectedIndex = -1;
            cbAVP.Text = string.Empty;
            cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);
                cbFarmers.SelectedIndexChanged -= cbFarmers_SelectedIndexChanged;
                cbFarmers.DataSource = UtilClass.GetFarmers(avpID);
                cbFarmers.ValueMember = "ID";
                cbFarmers.DisplayMember = "Name";
                cbFarmers.SelectedIndex = -1;
                cbFarmers.Enabled = true;
                cbFarmers.SelectedIndexChanged += new EventHandler(cbFarmers_SelectedIndexChanged);
            }
            else
            {
                avpID = 0;
                farmer = null;
                cbFarmers.DataSource = null;
                tbArea.Text = string.Empty;
            }
        }

        private void cbFarmers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbFarmers.SelectedIndex >= 0)
            {
                int fid = Convert.ToInt32(cbFarmers.SelectedValue);
                farmer = DBClass.Farms.Where(wh => wh.ID == fid && wh.AVPID == avpID).FirstOrDefault();
                if (farmer != null)
                {
                    tbArea.Text = farmer.Area.Value.ToString();
                    tbArea.Enabled = true;
                }
            }
            else
            {
                tbArea.Text = string.Empty;
            }
        }

        private void newFarmerButton_Click(object sender, EventArgs e)
        {
            tbNewFarmer.Location = cbFarmers.Location;
            tbNewFarmer.Size = cbFarmers.Size;
            this.Controls.Add(tbNewFarmer);
            cbFarmers.Visible = false;
            tbArea.Enabled = true;
            tbArea.Text = string.Empty;
            farmer = null;
            updaterec = false;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            decimal area;
            tbArea.Text.GetDecimalOrDefault(out area);

            //if ((string.IsNullOrEmpty(tbNewFarmer.Text) || string.IsNullOrEmpty(cbAVP.Text)) || string.IsNullOrEmpty(tbArea.Text))
            //{
            //    MessageBox.Show("Отсутствуют необходимые параметры.");
            //    return;
            //}

            if (updaterec && farmer != null)
            {
                if (string.IsNullOrEmpty(cbFarmers.Text))
                {
                    MessageBox.Show(msgClass.msgUncorrectData, msgClass.msgAchtung);
                    tbNewFarmer.Focus();
                    tbNewFarmer.SelectionStart = 0;
                    tbNewFarmer.SelectionLength = tbNewFarmer.Text.Length;
                    return;
                }

                farmer.Area = area;
                farmer.Name = cbFarmers.Text;
            }
            else if (!updaterec && farmer == null)
            {
                if (string.IsNullOrEmpty(tbNewFarmer.Text) || DBClass.Farms.Where(wh => wh.Name == tbNewFarmer.Text && wh.AVPID == avpID).Select(se => se.Name).ToArray().Contains(tbNewFarmer.Text))
                {
                    MessageBox.Show(msgClass.msgUncorrectData,msgClass.msgAchtung);
                    tbNewFarmer.Focus();
                    tbNewFarmer.SelectionStart = 0;
                    tbNewFarmer.SelectionLength = tbNewFarmer.Text.Length;
                    return;
                }

                farmer = new tb_farm
                {
                    Area = area,
                    Name = tbNewFarmer.Text,
                    AVPID = avpID
                };
                DBClass.RESPContext.tb_farms.InsertOnSubmit(farmer);
                tbNewFarmer.Dispose();
                cbFarmers.SelectedIndex = -1;
            }
            try
            {
                if (DBClass.RESPContext.GetChangeSet().Deletes.Count > 0 || DBClass.RESPContext.GetChangeSet().Inserts.Count > 0 || DBClass.RESPContext.GetChangeSet().Updates.Count > 0)
                {
                    DBClass.RESPContext.SubmitChanges();

                    if (MessageBox.Show(msgClass.msgSubmitChanges + " Выйти?", msgClass.msgAchtung, MessageBoxButtons.YesNo) == DialogResult.Yes)
                        Close();
                }
            }
            catch
            {
                MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
            }

            //this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (farmer != null)
            {
                if (MessageBox.Show(msgClass.msgDeleteQuestion, msgClass.msgAchtung, MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    DBClass.Farms.DeleteOnSubmit(farmer);
                    DBClass.RESPContext.SubmitChanges();
                    tbArea.Text = tbNewFarmer.Text = string.Empty;
                    cbFarmers.SelectedIndexChanged -= cbFarmers_SelectedIndexChanged;
                    cbFarmers.DataSource = UtilClass.GetFarmers(avpID);
                    cbFarmers.ValueMember = "ID";
                    cbFarmers.DisplayMember = "Name";
                    cbFarmers.SelectedIndex = -1;
                    cbFarmers.Text = string.Empty;
                    cbFarmers.SelectedIndexChanged += new EventHandler(cbFarmers_SelectedIndexChanged);
                }
            }
        }


    }
}
